package org.rency.spring.cloud.boot.mvc.common;

import org.apache.commons.lang.StringUtils;
import org.rency.common.utils.domain.RespBody;
import org.rency.common.utils.domain.SYSDICT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * ${todo}
 *
 * @author: Created by user_rcy@163.com on 2016/8/19.
 * @version: V1.0.0
 * @since JDK 1.8
 */
public class BaseAction implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 8745714826482655611L;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    protected final RespBody respBody = new RespBody();

    protected final Map<String, Object> respData = new HashMap<String, Object>();

    public BaseAction(){
        respBody.setMessage(Constants.CHINESE_SUCCESS_MSG);
        respBody.setSuccess(true);
        respBody.setData(respData);
        this.clear();
    }

    protected ModelAndView view(String prefix, String url){
        StringBuilder builder = new StringBuilder();
        builder.append(prefix);
        builder.append("/");
        builder.append(url);
        return new ModelAndView(builder.toString(),Constants.RESPONSE_KEY,respBody);
    }

    /**
     * 跳转
     * @param url
     * @return
     */
    protected ModelAndView redirect(String url){
        url = StringUtils.isBlank(url) ? Constants.URL_HOME : url;
        ModelAndView mv = new ModelAndView("redirect:"+url,Constants.RESPONSE_KEY,respBody);
        return mv;
    }

    /**
     * 处理成功
     * @param returnMessage
     */
    protected void success(String returnMessage){
        respBody.setSuccess(true);
        respBody.setMessage(returnMessage);
    }

    /**
     * 处理失败
     * @param errorMessage
     */
    protected void failed(String errorMessage){
        respBody.setSuccess(false);
        respBody.setMessage(errorMessage);
    }

    protected String getFromAddr(HttpHeaders headers,HttpServletRequest request){
        String callback = request.getParameter(SYSDICT.URL_PARAM_CALLBACK_KEY);
        if(StringUtils.isNotBlank(callback)){
            return callback;
        }
        String fromUrl = null;
        URI uri = headers.getLocation();
        if(uri != null){
            fromUrl = uri.toString();
        }
        return fromUrl;
    }

    protected String getAuthenticationMessage(Exception exception){
        String message = null;
        if(exception instanceof UsernameNotFoundException){
            message = "用户不存在";
        }else if(exception instanceof BadCredentialsException){
            message = "密码错误";
        }else if(exception instanceof LockedException){
            message = "账户被锁定";
        }else if(exception instanceof DisabledException){
            message = "账户被禁用";
        }else if(exception instanceof CredentialsExpiredException){
            message = "密码过期";
        }else{
            message = "登录失败";
        }
        return message;
    }

    /**
     * 清空报文体
     */
    protected void clear(){
        respData.clear();
        respBody.setMessage(null);
        respBody.setCallback("");
    }
}